package com.exam.paper.controller;

import com.exam.common.entity.Cascader;
import com.exam.common.entity.PageRequest;
import com.exam.common.entity.Request;
import com.exam.common.entity.StatusCode;
import com.exam.common.exception.ExamException;
import com.exam.paper.pojo.Category;
import com.exam.paper.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName : CategoryController
 * @Description : 分类Controller
 * @Author : WHB
 */

@RestController
@RequestMapping("category")
@Api(description = "分类信息API接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询试题分类信息
     * @param page
     * @param size
     * @return
     */
    @GetMapping("pagelist/{page}/{size}")
    @ApiOperation(value="分页获取分类信息列表", notes="分页获取分类信息列表")
    public ResponseEntity<PageRequest<Category>> queryCategoryByPage(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ){
        return ResponseEntity.ok(categoryService.queryCategoryByPage(page,size));
    }

    /**
     * 获取父分类列表
     * @return
     */
    @GetMapping("pcategory")
    @ApiOperation(value="获取父分类列表", notes="获取父分类列表")
    public ResponseEntity<List<Category>> queryPCategory(){
        return ResponseEntity.ok(categoryService.queryPCategory());
    }

    /**
     * 新增分类
     * @param data
     * @return
     */
    @PostMapping("category")
    @ApiOperation(value="新增分类", notes="新增分类")
    public Request saveCategory(@RequestBody String data){
        try {
            categoryService.saveCategory(data);
            return new Request(true, StatusCode.OK,"新增分类成功");
        }catch (ExamException e){
            System.out.println(e);
            return new Request(false, StatusCode.ERROR,"新增分类失败");
        }
    }


    /**
     * 修改分类
     * @param data
     * @return
     */
    @PutMapping("category")
    @ApiOperation(value="修改分类", notes="修改分类")
    public Request editCategory(@RequestBody String data){
        try {
            categoryService.editCategory(data);
            return new Request(true, StatusCode.OK,"修改分类成功");
        }catch (ExamException e){
            System.out.println(e);
            return new Request(false, StatusCode.ERROR,"修改分类失败");
        }
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation(value="删除分类", notes="删除分类")
    public Request deleteCategroy(@PathVariable("id") Long id){
        categoryService.deleteCategroy(id);
        return new Request(true,StatusCode.OK,"删除成功");
    }

    /**
     * 通过ID获取分类
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation(value="通过ID获取分类", notes="通过ID获取分类")
    public Request queryCategoryById(@PathVariable("id") Long id){
        return new Request(true,StatusCode.OK,"查找成功",categoryService.queryCategoryById(id));
    }

    /**
     * 获取层级分类列表
     * @return
     */
    @GetMapping("cascader")
    @ApiOperation(value="层级列表", notes="层级列表")
    public Request queryCascader(){
        return new Request(true,StatusCode.OK,"获取成功",categoryService.queryCascader());
    }
}
